<template>
	<view class="scrool-page">
		<view class="list-cont" v-if="!tabflag">
			<scroll-view class="scrool-more" style="height: 100%" scroll-y="true" scroll-with-animation="true">

				<view v-for="(item,index) in productList" :key="item.id">
					<uni-card :title="'序号:'+(index+1)" v-if="item.attr10 =='1' || status == '3'"
						style='background-color: darkgrey;'>
						<text class="uni-body">物料编码：{{ item.itemCode }}</text><br>
						<text class="uni-body">数量：{{ item.qty }}</text><br>
						<text class="uni-body">库位：{{ item.attr9 }}</text><br>
						<text class="uni-body">批次：{{ item.lotinfo }}</text><br>
						<text class="uni-body">箱码：{{ item.boxCode }}</text><br>
					</uni-card>

					<uni-card :title="'序号:'+(index+1)" v-else>
						<text class="uni-body">物料编码：{{ item.itemCode }}</text><br>
						<text class="uni-body">数量：{{ item.qty }}</text><br>
						<text class="uni-body">库位：{{ item.attr9 }}</text><br>
						<text class="uni-body">批次：{{ item.lotinfo }}</text><br>
						<text class="uni-body">箱码：{{ item.boxCode }}</text><br>
					</uni-card>
				</view>
			</scroll-view>
		</view>

		<view class="footer" v-if="!tabflag">
			<button type="default" size='mini' @click="reduceData" style="width:48%; margin-left:1%;"
				v-if="!userflag">减码</button>
			<button type="default" size='mini' @click="reduceData" style="width:48%; margin-left:1%;" v-else>加箱</button>
			<!-- <button type="default" size='mini' @click="nextStep" style="width:49%; margin-left:1%;">下一步</button> -->
			<button type="default" size='mini' @click="submit" style="width:49%; margin-left:1%;">完成</button>
		</view>

		<view class="list-cont" v-if="tabflag">
			<scroll-view class="scrool-more" style="height: 100%" scroll-y="true" scroll-with-animation="true">
				<view v-for="(item,index) in productList" :key="item.id">
					<uni-card :title="'产成品序号:'+(index+1)" v-if="item.areaCode == null || item.areaCode=='' ">
						<text class="uni-body">物料编码：{{ item.itemCode }}</text><br>
						<text class="uni-body">数量：{{ item.qty }}</text><br>
						<text class="uni-body">箱码：{{ item.boxCode }}</text><br>
					</uni-card>

					<uni-card :title="'产成品序号:'+(index+1)" v-else style='background-color: darkgrey;'>
						<text class="uni-body">物料编码：{{ item.itemCode }}</text><br>
						<text class="uni-body">数量：{{ item.codeEqualQty }}</text><br>
						<text class="uni-body">箱码：{{ item.boxCode }}</text><br>
					</uni-card>
				</view>
			</scroll-view>
		</view>

		<view class="footer" v-if="tabflag">
			<button type="default" size='mini' @click="lastStep" style="width:48%; margin-left:1%;">上一步</button>
			<button type="default" size='mini' @click="submit" style="width:49%; margin-left:1%;">完成</button>
		</view>

		<uni-popup ref="alertDialog" type="dialog" :is-mask-click="false">
			<uni-popup-dialog type="success" cancelText="关闭" confirmText="确定" title="提示" :before-close="true"
				:content="content" @confirm="dialogConfirm" @close="dialogClose">
			</uni-popup-dialog>
		</uni-popup>

		<uni-popup ref="reduceDialog" type="dialog" :is-mask-click="false">
			<uni-popup-dialog type="success" cancelText="关闭" confirmText="确定" title="提示" :before-close="true"
				:content="reduceContent" @confirm="reduceConfirm" @close="reduceClose">
			</uni-popup-dialog>
		</uni-popup>

		<uni-popup ref="messageDialog" type="dialog" :is-mask-click="false">
			<uni-popup-dialog type="success" cancelText="关闭" confirmText="确定" title="提示" :before-close="true"
				:content="messageeContent" @confirm="messageConfirm" @close="messageClose">
			</uni-popup-dialog>
		</uni-popup>

		<uni-popup ref="dataDialog" type="dialog" :is-mask-click="false">
			<uni-popup-dialog type="success" cancelText="关闭" confirmText="确定" title="提示" :before-close="true"
				:content="dataContent" @confirm="dataConfirm" @close="dataClose">
			</uni-popup-dialog>
		</uni-popup>
	</view>

</template>

<script>
	import Vue from 'vue'
	import {
		getToken
	} from '@/utils/auth'
	import common from '../../pages/common/common.js'
	import {
		getDicts
	} from "@/api/system/dict/data";
	const printer = uni.requireNativePlugin('LcPrinter');
	const modal = uni.requireNativePlugin('modal');
	var globalEvent = uni.requireNativePlugin('globalEvent');
	var main, receiver, filter;
	var ScanDeviceClass = plus.android.importClass("android.device.ScanDevice");
	var scanDevice;
	export default {
		data() {
			return {
				productList: [],
				list: [],
				content: '',
				code: '',
				qty: '',
				boxCode: '',
				count: 0,
				userflag: false,
				codeStr: '',
				popFlag: false,
				reduceContent: '',
				reduceCode: '',
				reduceNumkber: '',
				reducePatch: '',
				reduceCount: 0,
				barcodeIdList: [],
				dataContent: '检测到有缓存数据是否加载？',
				messageeContent: '确认删除数据吗?',
				sumbitFlag: true,
				baseUrl: getApp().globalData.config.baseUrl,
				userName: this.$store.state.user.name,
				orgId: getApp().globalData.config.orgId,
				tabflag: false,
				pkOrder: '',
				id: '',
				title: '',
				status: '',
				index: '',
				areaCode: '',
				dictValue: '',
				docType: '',
				transferId: '',
				docNo: '',


			}
		},

		watch: {
			popFlag: {
				handler(newValue, oldValue) {
					if (newValue) {
						this.$refs.alertDialog.open();
						this.content = '物料编码：' + this.code + "\n" + "数量：" + this.codeEqualQty + "\n" + '箱码：' + this
							.boxCode + '\n';
					}
				}
			}

		},

		onShow() {
			scanDevice = new ScanDeviceClass();
			scanDevice.openScan(); // 打开扫描
			scanDevice.setOutScanMode(0); // 扫描模式				
			this.initScan();
			this.registerScan();
		},

		created: function() {
			this.orgId = uni.getStorageSync('orgId');
			this.content = '';
			this.code = '';
			this.codeEqualQty = '';
			this.boxCode = '';
			this.count = 0;
			this.userflag = false;
			this.codeStr = '';
			this.popFlag = false;
			this.reduceContent = '';
			this.reduceCode = '';
			this.reduceNumkber = '';
			this.reducePatch = '';
			this.reduceCount = 0;
			this.barcodeIdList = [];
		},

		onHide: function() {
			this.unregisterScan();
		},

		destroyed: function() {
			this.unregisterScan();
		},

		onLoad: function(option) {
			this.id = option.id;
			this.pkOrder = option.pkOrder;
			this.title = option.title;
			this.status = option.status;
			this.index = option.index;
			uni.setNavigationBarTitle({
				title: this.title
			});
			// 添加打印状态监听
			globalEvent.addEventListener('onPrintCallback', function(e) {
				// uni.showToast({
				// 	title: 'state: ' + JSON.stringify(e),
				// 	duration: 2000
				// });
				if (e.key == 0) {
					console.log('reworkIn', '打印成功');
					// uni.showToast({
					// 	title: '打印成功',
					// 	duration: 2000
					// });
				} else if (e.key == 3) {
					uni.showToast({
						title: '缺纸',
						duration: 2000
					});
				}
			});
			//打印机版本获取回调
			globalEvent.addEventListener('onVersion', function(e) {
				// uni.showToast({
				// 	title: 'version: ' + JSON.stringify(e),
				// 	duration: 2000
				// });
			});

			globalEvent.addEventListener('getsupportprint', function(e) {
				//console.log('key: ' + JSON.stringify(e));
				// uni.showToast({
				// 	title: 'key: ' + JSON.stringify(e),
				// 	duration: 2000
				// });
			});

			this.getDicts("cross_org_inouttype").then(response => {
				if (response.data.length > 0) {
					this.dictValue = response.data[0].dictValue
				} else {
					uni.showToast({
						icon: 'none',
						title: '数据为空',
						duration: 3000
					})
				}

				// this.statusOptions= response.data;
			});

		},

		mounted() {
			//this.test();
			const value = uni.getStorageSync('product_recpt_rework' + this.id);
			if (value.length > 0) {
				this.list = value;
				this.$refs.dataDialog.open();
			} else {
				this.list = [];
				this.productList = [];
				this.query(this.pkOrder);
			}
			this.orgId = uni.getStorageSync('orgId');
		},

		onBackPress(options) {
			var that = this;
			if (options.from === 'navigateBack') {
				return false;
			}
			uni.showLoading({
				title: '加载中',
				mask: true
			});
			//that.unregisterScan();
			//player.stop();
			let pages = getCurrentPages(); //获取所有页面栈实例列表
			let nowPage = pages[pages.length - 1]; //当前页页面实例
			let prevPage = pages[pages.length - 2]; //上一页页面实例
			var count = 0;
			for (var i = 0; i < that.productList.length; i++) {
				if (that.productList[i].attr10 == '1') {
					count = count + 1;
				}
			}
			if (that.sumbitFlag && that.status != '3' && count > 0) {
				try {
					uni.setStorageSync('product_recpt_rework' + that.id, that.productList);
				} catch (e) {
					console.log(e.message);
				}
			}
			prevPage.$vm.chooseTab(that.index);
			setTimeout(function() {
				uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
					delta: 1
				});
			}, 1000);
			//隐藏加载框
			uni.hideLoading();
			return true;
		},

		methods: {
			initScan(popFlag) {
				let _this = this;
				main = plus.android.runtimeMainActivity(); //获取activity  
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				filter = new IntentFilter();
				filter.addAction("scan.rcv.message"); // 换你的广播动作  
				receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
					onReceive: function(context, intent) {
						plus.android.importClass(intent);
						let code = intent.getByteArrayExtra('barocode');
						let codeStr = common.utf8ByteToUnicodeStr(code);
						scanDevice.stopScan(); // 停止扫描	

						var result = common.scanCode(codeStr);
						if (result[0] == 'K') {
							_this.resYes(result[1]);
						} else {
							_this.buttonScan(codeStr);
							_this.viewScan(codeStr);
						}
						// if(_this.status =='2'){
						// 	uni.showToast({
						// 		title: '任务已挂起，请先恢复',
						// 		icon: 'none',
						// 		duration: 3000
						// 	});
						// 	return;
						// }

						// if(_this.status =='3'){
						// 	uni.showToast({
						// 		title: '任务已完成，不能修改',
						// 		icon: 'none',
						// 		duration: 3000
						// 	});
						// 	return;
						// }
						// var result = common.scanCode(codeStr);
						// if(result[0] == "K"){
						// 	_this.resYes(result[1]);
						// }else if(result[0] == "C"){
						// 	_this.buttonScan(result[1]);
						// 	_this.viewScan(result[1]);
						// }else{
						// 	uni.showToast({
						// 		title: '请正确扫描箱号',
						// 		icon: 'none',
						// 		duration: 4000
						// 	});
						// 	return;
						// }															
					}
				});
			},

			//扫描减码
			reduceData() {
				if (this.status == '2') {
					uni.showToast({
						title: '任务已挂起，请先恢复',
						icon: 'none',
						duration: 3000
					});
					return;
				}

				if (this.status == '3') {
					uni.showToast({
						title: '任务已完成，不能修改',
						icon: 'none',
						duration: 3000
					});
					return;
				}
				if (this.userflag) {
					this.userflag = false;
				} else {
					this.userflag = true;
				}
			},

			//如果是机器上面的扫描按钮，是增加数据
			buttonScan(codeStr) {
				console.log(codeStr)
				if (this.status == '2') {
					uni.showToast({
						title: '任务已挂起，请先恢复',
						icon: 'none',
						duration: 3000
					});
					return;
				}

				if (this.status == '3') {
					uni.showToast({
						title: '任务已完成，不能修改',
						icon: 'none',
						duration: 3000
					});
					return;
				}
				var that = this;
				if (that.userflag) {
					return;
				}
				if (codeStr == null || codeStr == '') {
					uni.showToast({
						title: '请正确扫描编码',
						icon: 'none',
						duration: 3000
					});
					return;
				}
				if (that.areaCode == null || that.areaCode == '') {
					uni.showToast({
						title: '请先扫描库位',
						icon: 'none',
						duration: 3000
					});
					return;
				}

				if (that.dictValue == that.docType) { //跨组织调入单

					if (!that.popFlag) {
						that.popFlag = true;
					}
					that.$refs.alertDialog.open();
					var result = common.scanCode(codeStr);
					if (result[0] == "P") {
						var flag = false;
						for (var i = 0; i < that.productList.length; i++) {
							if (that.productList[i].itemCode == result[1]) {
								flag = true;
								break;
							}
						}
						if (flag) {
							that.code = result[1];
							that.count = parseInt(that.count) + 1;
							that.content = '物料编码：' + that.code + "\n" + "数量：" + that.codeEqualQty + "\n" + '箱码：' + that
								.boxCode + '\n';
						} else {
							uni.showToast({
								title: '此物料编码不存在',
								icon: 'none',
								duration: 4000
							});
							return;
						}
					} else if (result[0] == "Q") {
						that.count = parseInt(that.count) + 1;
						//that.content = '物料编码：' +that.code+"\n"+"数量："+that.codeEqualQty+"\n"+'箱码：'+that.boxCode+'\n';
						// var flag = false;
						// for (var i = 0; i < that.productList.length; i++) {
						// 	if (that.productList[i].qty == result[1]) {
						// 		flag = true;
						// 		break;
						// 	}
						// }
						// if (flag) {
						// 	that.codeEqualQty = result[1];
						// 	that.count = parseInt(that.count) + 1;
						// 	that.content = '物料编码：' + that.code + "\n" + "数量：" + that.codeEqualQty + "\n" + '箱码：' + that
						// 		.boxCode + '\n';
						// } else {
						// 	uni.showToast({
						// 		title: "箱容数与该列表中箱容数不匹配",
						// 		icon: 'none',
						// 		duration: 5000
						// 	});
						// 	return;
						// }


						console.log(result[1])
						uni.request({
							url: that.baseUrl + '/system/TransferIn/checksNumber',
							data: {
								itemCode: that.productList[0].itemCode,
								Qty: result[1],
								orgId: that.orgId,
							},
							method: 'POST',
							header: {
								'authorization': getToken()
							},
							dataType: 'json',
							success: (res) => {
								console.log(res)
								if (res.data.code == '200' || res.data.code == 200) {
									if (res.data.data != true) {
										uni.showToast({
											title: '未找到与当前匹配的箱容数,请在物料档案绑定对应此数量的箱型.',
											icon: 'none',
											duration: 4000
										});
									} else {
										that.codeEqualQty = result[1];
										that.count = parseInt(that.count) + 1;
										that.content = '物料编码：' + that.code + "\n" + "数量：" + that.codeEqualQty +
											"\n" + '箱码：' + that
											.boxCode + '\n';
									}

								} else {
									//隐藏加载框
									uni.hideLoading();
									uni.showToast({
										title: res.data.msg,
										icon: 'none',
										duration: 2000
									});
									return;
								}

							},
						})
					} else if (result[0] == "C") {
						var flag = false;
						for (var i = 0; i < that.productList.length; i++) {
							if (that.productList[i].boxCode == result[1]) {
								flag = true;
								break;
							}
						}
						if (flag) {
							uni.showToast({
								title: '此数据已存在',
								icon: 'none',
								duration: 4000
							});
							return;
						} else {
							var flag = false;
							console.log(that.productList)
							for (var i = 0; i < that.productList.length; i++) {
								var data = that.productList[i];
								console.log(data.lotinfo.substring(3, 12))
								if (data.lotinfo.substring(3, 12) == result[1].substring(0, 9) && (data.boxCode == null ||
										data.boxCode ==
										'')) {
									flag = true;
								}
							}
							if (flag) {
								that.allCode = codeStr;
								that.boxCode = result[1];
								that.content = '物料编码：' + that.code + "\n" + "数量：" + that.codeEqualQty + "\n" + '箱码：' + that
									.boxCode + '\n';
							} else {
								uni.showToast({
									title: '此批次的数据不存在或者已经扫描完成',
									icon: 'none',
									duration: 5000
								});
								return;
							}
						}
					} else {
						uni.showToast({
							title: '请正确扫描编码',
							icon: 'none',
							duration: 5000
						});
						return;
					}
				} else {


					uni.showLoading({
						title: '加载中',
						mask: true
					});

					var flag = 0;
					var data = '';
					for (var i = 0; i < that.productList.length; i++) {
						data = that.productList[i];
						//调入单不校验箱码，需要扫三码，库位也需要提前扫
						if (data.boxCode == codeStr) {
							if (data.attr10 == '1') {
								flag = 1;
							} else {
								flag = 2;
							}
							break;
						}
					}
					if (flag == 0) {
						//隐藏加载框
						uni.hideLoading();
						uni.showToast({
							title: '此数据不存在',
							icon: 'none',
							duration: 4000
						});
					} else if (flag == 1) {
						//隐藏加载框
						uni.hideLoading();
						uni.showToast({
							title: '此箱号已扫描',
							icon: 'none',
							duration: 4000
						});
					} else {
						that.queryByBoxCode(codeStr, data);
						//隐藏加载框
						uni.hideLoading();
					}

				}

			},

			//如果是页面上的减码扫描，那就是删除数据
			viewScan(codeStr) {
				if (!this.userflag) {
					return;
				}
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				if (codeStr.substring(0, 1) == 'P' || codeStr.substring(0, 1) == 'Q') {
					//隐藏加载框
					uni.hideLoading();
					uni.showToast({
						title: '请扫描箱码',
						icon: 'error',
						duration: 2000
					});
					this.userflag = false;
				} else {

					if (this.dictValue == this.docType) { //跨组织调入单
						console.log('aa')

						var flag = 0;
						var data = '';
						for (var i = 0; i < this.productList.length; i++) {
							data = this.productList[i];
							if (data.boxCode == codeStr) {
								if (data.attr10 == '1') {
									flag = 1;
								} else {
									flag = 2;
								}
								break;
							}
						}
						if (flag == 0) {
							//隐藏加载框
							uni.hideLoading();
							uni.showToast({
								title: '此数据不存在',
								icon: 'none',
								duration: 4000
							});
						} else if (flag == 2) {
							//隐藏加载框
							uni.hideLoading();
							uni.showToast({
								title: '此箱号未扫描',
								icon: 'none',
								duration: 4000
							});
						} else {
							data.attr10 = '';
							data.attr9 = '';
							data.qty = '0';
							data.boxCode = '';
							this.prompt();
							//隐藏加载框
							uni.hideLoading();
						}
					} else {


						var flag = 0;
						var data = '';
						for (var i = 0; i < this.productList.length; i++) {
							data = this.productList[i];
							if (data.boxCode == codeStr) {
								if (data.attr10 == '1') {
									flag = 1;
								} else {
									flag = 2;
								}
								break;
							}
						}
						if (flag == 0) {
							//隐藏加载框
							uni.hideLoading();
							uni.showToast({
								title: '此数据不存在',
								icon: 'none',
								duration: 4000
							});
						} else if (flag == 2) {
							//隐藏加载框
							uni.hideLoading();
							uni.showToast({
								title: '此箱号未扫描',
								icon: 'none',
								duration: 4000
							});
						} else {
							data.attr10 = '';
							data.attr9 = '';
							// data.boxCode = '';
							this.prompt();
							//隐藏加载框
							uni.hideLoading();
						}
					}
				}
			},

			query(id) {
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				var that = this;
				uni.request({
					url: that.baseUrl + '/system/TransferIn/find-one-by-id?id=' + id,
					// data:{
					// 	userName:that.userName,
					// 	id:id,
					// 	orgId:that.orgId,
					// },
					method: 'POST',
					header: {
						'authorization': getToken()
					},
					dataType: 'json',
					success: (res) => {
						console.log(res)
						if (res.data.code = '200') {
							that.docType = res.data.header.docType
							that.productList = res.data.details;
							that.transferId = res.data.header.id
							that.docNo = res.data.header.docNo
							//隐藏加载框
							uni.hideLoading();
						} else {
							//隐藏加载框
							uni.hideLoading();
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 3000
							});
							return;
						}
					},
				})
			},

			queryByBoxCode(boxCode, record) {
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				var that = this;
				//根据箱号查询条码主档
				uni.request({
					url: that.baseUrl + '/system/barcode/box',
					data: {
						userName: that.userName,
						boxCode: boxCode,
						orgId: that.orgId,
					},
					method: 'POST',
					header: {
						'authorization': getToken()
					},
					dataType: 'json',
					success: (res) => {
						if (res.data.code == '200' || res.data.code == 200) {
							var result = res.data.data;
							// if(result.isFreeze == '1'){
							// 	uni.showToast({
							// 		title: '此箱已被占用，不能组托',
							// 		icon: 'none',
							// 		duration: 5000
							// 	});
							// 	return;
							// }
							// if(result.isStocked == '0'){
							// 	uni.showToast({
							// 		title: '此箱不在库，不能组托',
							// 		icon: 'none',
							// 		duration: 5000
							// 	});
							// 	return;
							// }
							// if(result.occupation == '1'){
							// 	uni.showToast({
							// 		title: '此箱已冻结，不能组托',
							// 		icon: 'none',
							// 		duration: 5000
							// 	});
							// 	return;
							// }
							record.attr10 = '1';
							record.attr9 = this.areaCode;
							that.prompt();
							//隐藏加载框
							uni.hideLoading();
						} else {
							//隐藏加载框
							uni.hideLoading();
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 2000
							});
							return;
						}

					},
				})
			},

			resYes(warehouseLocation) {
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				uni.request({
					url: this.baseUrl + '/system/area/ResYes',
					data: {
						areaCode: warehouseLocation,
						userName: this.userName,
						orgId: this.orgId,
					},
					method: 'POST',
					header: {
						'authorization': getToken()
					},
					dataType: 'json',
					success: (res) => {
						//隐藏加载框
						uni.hideLoading();
						if (res.data) {
							this.areaCode = warehouseLocation;
							uni.setNavigationBarTitle({
								title: this.title.split('-')[1] + '(' + this.areaCode + ")"
							});
						} else {
							uni.showToast({
								title: warehouseLocation + '不存在或者不可用',
								icon: 'none',
								duration: 3000
							});
							return;
						}

					},
				})
			},

			nextStep() {
				this.tabflag = true;
				uni.setNavigationBarTitle({
					title: this.title
				});
			},

			lastStep() {
				this.tabflag = false;
				uni.setNavigationBarTitle({
					title: this.title + '上架'
				});
			},

			//组托
			submit() {
				if (this.status == '2') {
					uni.showToast({
						title: '任务已挂起，请先恢复',
						icon: 'none',
						duration: 3000
					});
					return;
				}

				if (this.status == '3') {
					uni.showToast({
						title: '任务已完成，不能修改',
						icon: 'none',
						duration: 3000
					});
					return;
				}
				//显示加载框
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				var that = this
				if (that.dictValue == that.docType) { //跨组织调入单


					var that = this;
					// var count = 0;
					// for (var i = 0; i < that.productList.length; i++) {
					// 	if (that.productList[i].attr10 == '1') {
					// 		count = count + 1;
					// 	}
					// }
					// if (count == that.productList.length) {
						let token = getToken();
						console.log(that.userName)
						console.log(that.productList)
						console.log(that.pkOrder)
						console.log(that.orgId)
						uni.request({
							url: that.baseUrl + '/system/TransferIn/cross-org-submit-from-pda',
							data: {
								executorCode: that.userName,
								boxes: that.productList,
								id: that.pkOrder, //单据数据库主键id
								orgId: that.orgId,
								transferId: that.transferId,
								docNo: that.docNo
							},
							method: 'POST',
							header: {
								'authorization': token
							},
							dataType: 'json',
							success: (res) => {
								console.log(res)
								var code = res.data.code;
								if (code == '200') {
									that.sumbitFlag = false;
									that.updateTask();
									//隐藏加载框
									uni.hideLoading();
								} else {
									//隐藏加载框
									uni.hideLoading();
									uni.showToast({
										title: res.data.msg,
										icon: 'none',
										duration: 4000
									});
									return;
								}
							}
						})
					// } else {
					// 	if (count > 0) {
					// 		//隐藏加载框
					// 		uni.hideLoading();
					// 		uni.showModal({
					// 			title: '提示',
					// 			content: '此返工入库单还未完成是否临时保存?',
					// 			success: function(res) {
					// 				if (res.confirm) {
					// 					uni.setStorageSync('product_recpt_rework' + that.id, that.productList);
					// 				} else {
					// 					that.sumbitFlag = false;
					// 					uni.removeStorageSync('product_recpt_rework' + that.id);
					// 				}
					// 				//that.unregisterScan();
					// 				let pages = getCurrentPages(); //获取所有页面栈实例列表
					// 				let nowPage = pages[pages.length - 1]; //当前页页面实例
					// 				let prevPage = pages[pages.length - 2]; //上一页页面实例
					// 				prevPage.$vm.chooseTab(that.index);
					// 				setTimeout(function() {
					// 					uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
					// 						delta: 1
					// 					});
					// 				}, 1000);
					// 			}
					// 		})
					// 	} else {
					// 		uni.hideLoading();
					// 		uni.showToast({
					// 			title: '返工入库单未扫描任何数据，不能完成',
					// 			icon: 'none',
					// 			duration: 3000
					// 		});
					// 		return;
					// 	}
					// }



				} else {


					var that = this;
					var count = 0;
					for (var i = 0; i < that.productList.length; i++) {
						if (that.productList[i].attr10 == '1') {
							count = count + 1;
						}
					}
					if (count == that.productList.length) {
						let token = getToken();
						uni.request({
							url: that.baseUrl + '/system/TransferIn/rework-submit-from-pda',
							data: {
								executorCode: that.userName,
								boxCodes: that.productList,
								id: that.pkOrder, //单据数据库主键id
								orgId: that.orgId
							},
							method: 'POST',
							header: {
								'authorization': token
							},
							dataType: 'json',
							success: (res) => {
								var code = res.data.code;
								if (code == '200') {
									that.sumbitFlag = false;
									that.updateTask();
									//隐藏加载框
									uni.hideLoading();
								} else {
									//隐藏加载框
									uni.hideLoading();
									uni.showToast({
										title: res.data.msg,
										icon: 'none',
										duration: 3000
									});
									return;
								}
							}
						})
					} else {
						if (count > 0) {
							//隐藏加载框
							uni.hideLoading();
							uni.showModal({
								title: '提示',
								content: '此返工入库单还未完成是否临时保存?',
								success: function(res) {
									if (res.confirm) {
										uni.setStorageSync('product_recpt_rework' + that.id, that.productList);
									} else {
										that.sumbitFlag = false;
										uni.removeStorageSync('product_recpt_rework' + that.id);
									}
									//that.unregisterScan();
									let pages = getCurrentPages(); //获取所有页面栈实例列表
									let nowPage = pages[pages.length - 1]; //当前页页面实例
									let prevPage = pages[pages.length - 2]; //上一页页面实例
									prevPage.$vm.chooseTab(that.index);
									setTimeout(function() {
										uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
											delta: 1
										});
									}, 1000);
								}
							})
						} else {
							uni.hideLoading();
							uni.showToast({
								title: '返工入库单未扫描任何数据，不能完成',
								icon: 'none',
								duration: 3000
							});
							return;
						}
					}
				}



			},

			//更新代办任务状态为完成
			updateTask() {
				var that = this;
				//显示加载框
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				uni.request({
					url: that.baseUrl + '/system/wmsbacklog/update',
					data: {
						id: that.id,
						executor: that.userName,
						orgId: that.orgId,
						startTime: '',
						endTime: new Date().toISOString().slice(0, 10),
						status: '3' //完成状态
					},
					method: 'POST',
					header: {
						'authorization': getToken()
					},
					dataType: 'json',
					success: (res) => {
						//隐藏加载框
						uni.hideLoading();
						//that.unregisterScan();
						//player.stop();
						if (res.data.code == '200' || res.data.code == 200) {
							uni.showToast({
								title: '操作成功并且代办任务已完成',
								icon: 'none',
								duration: 3000
							});
							setTimeout(function() {
								let pages = getCurrentPages(); //获取所有页面栈实例列表
								let nowPage = pages[pages.length - 1]; //当前页页面实例
								let prevPage = pages[pages.length - 2];
								prevPage.$vm.chooseTab(that.index);
								uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
									delta: 1
								});
							}, 1000);
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 3000
							});
							return;
						}

					},
				})
			},

			makeBarCode(areaName) {
				var that = this;
				uni.request({
					url: that.baseUrl + '/system/barcode',
					data: {
						productList: that.productList,
						sourceBill: 'MD_ITEM_PANEL',
						orgId: that.orgId,
						userName: that.userName,
						defaultArea: areaName,
					},
					method: 'POST',
					header: {
						'authorization': getToken()
					},
					dataType: 'json',
					success: (res) => {
						if (res.data.code == '200' || res.data.code == 200) {
							var ret = printer.initPrinter({});
							// modal.toast({
							// 	message: ret,
							// 	duration: 1.5
							// });
							printer.printEnableMark({
								enable: true
							});
							printer.setConcentration({ //设置打印浓度。font_level 取值范围：1~39。
								level: 39
							});
							printer.setLineSpacing({
								spacing: 1
							});
							printer.getsupportprint();

							printer.setFontSize({
								fontSize: 0
							});
							printer.setTextBold({
								bold: true
							});
							// printer.printQR2({
							// 	text: res.data.msg,
							// 	height: 600,
							// 	offset: 1
							// });
							printer.printBarcode({
								text: res.data.msg,
								height: 500,
								barcodeType: 73,
								offset: 2
							});
							printer.start();
							//that.unregisterScan();
							setTimeout(function() {
								uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
									delta: 1
								});
							}, 1000);
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 2000
							});
							return;
						}

					},
				})
			},

			registerScan() {
				main.registerReceiver(receiver, filter);
			},

			unregisterScan() {
				main.unregisterReceiver(receiver);
			},

			dialogConfirm() {
				var that = this

				if (that.dictValue == that.docType) { //跨组织调入单
					//显示加载框
					uni.showLoading({
						title: '加载中',
						mask: true
					});
					//如果三个码都扫描完成
					var that = this;
					if (that.code == null || that.code == '') {
						uni.showToast({
							title: '请扫描物料编码',
							icon: 'error',
							duration: 2000
						});
						return;
					} else if (that.codeEqualQty == null || that.codeEqualQty == '') {
						uni.showToast({
							title: '请扫描数量',
							icon: 'error',
							duration: 2000
						});
						return;
					} else if (that.boxCode == null || that.boxCode == '') {
						uni.showToast({
							title: '请扫描箱码',
							icon: 'error',
							duration: 2000
						});
						return;
					} else {

						var flag = false;
						for (var i = 0; i < that.productList.length; i++) {
							var data = that.productList[i];
							if (data.itemCode == that.code && (data.boxCode == null || data.boxCode == '')) {
								flag = true;
								break;
							}
						}
						if (!flag) {
							uni.showToast({
								title: '物料编码为' + that.code + '的数据不存在或者已被扫描',
								icon: 'none',
								duration: 5000
							});
							return;
						}
						console.log(data.lotinfo.substring(3, 12))
						console.log(that.boxCode.substring(0, 9))
						var flags = false;
						for (var j = 0; j < that.productList.length; j++) {
							var data = that.productList[i];
							if (data.lotinfo.substring(3, 12) == that.boxCode.substring(0, 9)) {
								flags = true;
								break;
							}
						}
						if (!flags) {
							uni.showToast({
								title: '箱码为' + that.boxCode + '批次不正确',
								icon: 'none',
								duration: 5000
							});
							return;
						}


						let token = getToken();
						console.log(that.code)
						console.log(that.codeEqualQty)
						uni.request({
							url: that.baseUrl + '/system/item/checksNumber',
							data: {
								itemCode: that.code,
								boxQty: that.codeEqualQty
								// orgId: this.orgId,
								// code: that.code,
								// qty: that.codeEqualQty,
								// boxCode: that.boxCode,
								// sourceBill: 'MD_ITEM', //单据类型是物料
								// userName: this.userName,
							},
							method: 'POST',
							header: {
								'authorization': token
							},
							dataType: 'json',
							success: (res) => {
								console.log(res)
								uni.hideLoading();

								for (var i = 0; i < that.productList.length; i++) {
									var data = that.productList[i];
									//if(data.itemCode == that.code && data.qty == that.codeEqualQty && data.lotinfo == that.boxCode.substring(0,9)  && (data.boxCode == null || data.boxCode == '')){
									if (data.itemCode == that.code && (data.boxCode == null || data.boxCode ==
											'')) {
										data.qty = that.codeEqualQty;
										data.storageAreaCode = that.areaCode;
										//data.batchNumber = that.boxCode.substring()(0,9);
										data.boxCode = that.boxCode;
										// data.lotinfo = that.boxCode.substring(0, 9);
										data.allCode = that.allCode;

										data.attr10 = '1';
										data.attr9 = this.areaCode;
										break;
									}
								}
								that.code = '';
								that.codeEqualQty = '';
								that.boxCode = '';
								that.allCode = "";
								that.count = '0';
								that.popFlag = false;
								that.$refs.alertDialog.close();

							}
						})

					}

				} else {



					//如果三个码都扫描完成
					var that = this;
					if (that.code == null || that.code == '') {
						uni.showToast({
							title: '请扫描物料编码',
							icon: 'error',
							duration: 2000
						});
						return;
					} else if (that.codeEqualQty == null || that.codeEqualQty == '') {
						uni.showToast({
							title: '请扫描数量',
							icon: 'error',
							duration: 2000
						});
						return;
					} else if (that.boxCode == null || that.boxCode == '') {
						uni.showToast({
							title: '请扫描箱码',
							icon: 'error',
							duration: 2000
						});
						return;
					} else {
						let token = getToken();
						uni.request({
							url: that.baseUrl + '/system/barcode',
							data: {
								orgId: this.orgId,
								code: that.code,
								qty: that.codeEqualQty,
								boxCode: that.boxCode,
								sourceBill: 'MD_ITEM', //单据类型是物料
								userName: this.userName,
							},
							method: 'POST',
							header: {
								'authorization': token
							},
							dataType: 'json',
							success: (res) => {
								console.log(res)
								if (res.data.code == '200') {
									var tempList = {
										id: that.productList.length + 1,
										code: that.code,
										codeEqualQty: that.codeEqualQty,
										boxCode: that.boxCode,
										flag: false
									};
									that.productList.push(tempList);
									var json = JSON.parse(res.data.msg);
									that.barcodeIdList.push({
										id: that.productList.length + 1,
										barcodeId: json.barcodeId
									});
								} else if (res.data.code == '201') {
									var tempList = {
										id: that.productList.length + 1,
										code: that.code,
										codeEqualQty: that.codeEqualQty,
										boxCode: that.boxCode,
										flag: false
									};
									that.productList.push(tempList);
									this.barcodeIdList.push({
										id: this.productList.length + 1,
										barcodeId: res.data.msg
									});
								} else {
									uni.showToast({
										title: res.data.msg,
										icon: 'none',
										duration: 2000
									});
								}
								that.code = '';
								that.codeEqualQty = '';
								that.boxCode = '';
								that.count = '0';
								that.popFlag = false;
							}
						})
						that.$refs.alertDialog.close();
					}
				}
			},

			dialogClose() {
				var that = this
				if (that.dictValue == that.docType) { //跨组织调入单
					that.codeEqualQty = ""
					that.boxCode = ""
					that.code = ""
					this.$refs.alertDialog.close();
				} else {


					this.code = '';
					this.codeEqualQty = '';
					this.boxCode = '';
					this.count = '0';
					this.popFlag = false;
					this.$refs.alertDialog.close();
				}
			},

			reduceConfirm() {
				var tempList = [];
				for (var i = 0; i < this.productList.length; i++) {
					if (this.productList[i].boxCode == this.reducePatch) {

					} else {
						tempList.push(this.productList[i]);
					}
				}
				this.productList = [];
				this.productList = tempList;
				this.$refs.reduceDialog.close();
				this.reduceCode = '';
				this.reduceNumkber = '';
				this.reducePatch = '';
				this.reduceCount = 0;
				this.userflag = false;

			},

			reduceClose() {
				this.reduceCode = '';
				this.reduceNumkber = '';
				this.reducePatch = '';
				this.reduceCount = 0;
				this.userflag = false;
				this.$refs.reduceDialog.close();
			},

			dataConfirm() {
				this.productList = this.list;
				this.$refs.dataDialog.close();
				try {
					uni.removeStorageSync('product_recpt_rework' + this.id);
				} catch (e) {
					console.log(e.message);
				}
			},

			dataClose() {
				this.list = [];
				try {
					uni.removeStorageSync('product_recpt_rework' + this.id);
				} catch (e) {
					console.log(e.message);
				}
				this.productList = [];
				this.$refs.dataDialog.close();
				this.query(this.pkOrder);
			},

			messageConfirm() {
				scanDevice.stopScan(); // 停止扫描
				scanDevice.startScan();
				this.$refs.messageDialog.close();
			},

			messageClose() {
				this.userflag = false;
				this.$refs.messageDialog.close();
			},

			prompt(type) {
				var innerAudioContext = uni.createInnerAudioContext();
				innerAudioContext.autoplay = true;
				innerAudioContext.src = '/static/mp3/success.mp3';
				//innerAudioContext.play();//铃声文件的路径
				//innerAudioContext.stop();
				innerAudioContext.onPlay(() => {
					console.log('开始播放');
				});
				innerAudioContext.onError((res) => {
					console.log(res.errMsg);
					console.log(res.errCode);
				});
				innerAudioContext.onPause(function() {
					console.log('end');
					innerAudioContext.destroy()
				})
			},
		}
	}
</script>

<style lang="scss">
	.main {
		background-color: #fff;
		height: 300upx;
		align-items: center;
		margin-top: 10upx;
	}

	.text-content {
		font-size: 48rpx;
		text-align: center;
	}

	.city {
		/* #ifndef APP-PLUS-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		// width: 160rpx;
		margin-left: 4px;
		font-size: 26rpx;
	}

	.input-view {
		display: flex;
		flex-direction: row;
		flex: 1;
		//background-color: #f8f8f8;
		height: 30px;
		padding: 0 80px;
		flex-wrap: nowrap;
		margin: 7px 0;
		line-height: 30px;
		font-size: 26rpx;
	}

	page {
		width: 100%;
		height: 100%;
	}


	.scrool-page {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 100%;
	}

	.header-cont {
		width: 100%;
		height: 110rpx;
		line-height: 100rpx;
		text-align: center;
		// background: red;
	}

	.list-cont {
		flex: 1;
		position: relative;

		.scrool-more {
			position: absolute;
			left: 0;
			right: 0;
			top: 0;
			bottom: 0;
		}

		.list {
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			background: #e0e0e0;

		}
	}


	.footer {
		color: #fff;
		line-height: 100rpx;
		flex: 0 0 100rpx;
		/* 不放大不缩小固定100rpx */
	}
</style>